//
//  MKUITreePickerView.m
//  MKUI
//	
//  Created by sushangjin on 15/6/19.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import "MKUITreePickerView.h"
#import "UIScrollView+MKUI.h"
#define kMKUITreePickerViewNumberOfComponent 3

@interface MKUITreePickerView()<UIScrollViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
	UIPickerView *_pickerView;
}
@property(nonatomic,strong) UIScrollView *containerView;
@end

@implementation MKUITreePickerView
- (id)init{
	if (self=[self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 216)]) {
	}
	return self;
}
- (id)initWithFrame:(CGRect)frame{
	if(self=[super initWithFrame:frame]){
		self.containerView = [[UIScrollView alloc] init];
        self.containerView.scrollsToTop = NO;
		self.containerView.delegate = self;
		[self addSubview:self.containerView];
		_pickerView = [[UIPickerView alloc] init];
		_pickerView.dataSource = self;
		_pickerView.delegate = self;
		[self.containerView addSubview:_pickerView];
	}
	return self;
}
- (void)layoutSubviews{
	[super layoutSubviews];
	CGRect bounds = self.bounds;
	//
	CGRect f1 = bounds;
	self.containerView.frame = f1;
	[self _updateScrollViewContentSize];
	//滚动到最后
	CGPoint offset = CGPointZero;
	offset.x = self.containerView.contentSize.width-bounds.size.height;
	offset.x = MAX(offset.x,self.containerView.mk_contentOffsetOfMinX);
	offset.x = MIN(offset.x,self.containerView.mk_contentOffsetOfMaxX);
	self.containerView.contentOffset = offset;
}
- (void)_updateScrollViewContentSize{
	CGRect bounds = self.bounds;
	CGRect f2 = bounds;
	NSInteger numberOfComponents = [self.pickerView numberOfComponents];
	NSInteger maxComponentsInScreen = kMKUITreePickerViewNumberOfComponent;
	if(numberOfComponents>maxComponentsInScreen){
		CGFloat componentWidth = bounds.size.width/maxComponentsInScreen;
		f2.size.width = numberOfComponents*componentWidth;
	}
	self.pickerView.frame = f2;
	self.containerView.contentSize = f2.size;
}
- (UIPickerView *)pickerView{
	return _pickerView;
}
#pragma mark - delegate:UIScrollViewDelegate
- (void)scrollToComponentEdge{//滚动到分组的边缘
	NSInteger maxComponentsInScreen = kMKUITreePickerViewNumberOfComponent;
	CGRect bounds = self.bounds;
	CGFloat componentWidth = bounds.size.width/maxComponentsInScreen;
	CGPoint offset = self.containerView.contentOffset;
	NSInteger component = floor(offset.x/componentWidth);
	CGFloat dis = offset.x-component*componentWidth;
	if(dis<componentWidth/2){//往左移动
		offset.x = component*componentWidth;
	}else{
		offset.x = (component+1)*componentWidth;
	}
	offset.x = MAX(offset.x,self.containerView.mk_contentOffsetOfMinX);
	offset.x = MIN(offset.x,self.containerView.mk_contentOffsetOfMaxX);
	[self.containerView setContentOffset:offset animated:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if(!decelerate){
		[self scrollToComponentEdge];
	}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	[self scrollToComponentEdge];
}
#pragma mark - delegate:UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	if([self.dataSource respondsToSelector:_cmd]){
		return [self.dataSource numberOfComponentsInPickerView:pickerView];
	}
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	if([self.dataSource respondsToSelector:_cmd]){
		return [self.dataSource pickerView:pickerView numberOfRowsInComponent:component];
	}
	return 0;
}
#pragma mark - delegate:UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
	if([self.delegate respondsToSelector:_cmd]){
		return [self.delegate pickerView:pickerView widthForComponent:component];
	}
	CGRect bounds = pickerView.bounds;
	CGFloat width = bounds.size.width/[pickerView numberOfComponents];
	return width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
	if([self.delegate respondsToSelector:_cmd]){
		return [self.delegate pickerView:pickerView rowHeightForComponent:component];
	}
	return 44;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	if([self.delegate respondsToSelector:_cmd]){
		return [self.delegate pickerView:pickerView titleForRow:row forComponent:component];
	}
	return nil;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0){
	if([self.delegate respondsToSelector:_cmd]){
		return [self.delegate pickerView:pickerView attributedTitleForRow:row forComponent:component];
	}
	return nil;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
	if([self.delegate respondsToSelector:_cmd]){
		return [self.delegate pickerView:pickerView viewForRow:row forComponent:component reusingView:view];
	}
	return [UIView new];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	if([self.delegate respondsToSelector:_cmd]){
		[self.delegate pickerView:pickerView didSelectRow:row inComponent:component];
	}
	CGPoint offsetOld = self.containerView.contentOffset;
	[self _updateScrollViewContentSize];
	self.containerView.contentOffset = offsetOld;

	//调整row所在component的位置
	NSInteger maxComponentsInScreen = kMKUITreePickerViewNumberOfComponent;
	NSInteger numberOfComponents = [pickerView numberOfComponents];
	CGPoint offset = CGPointZero;
	if(numberOfComponents>maxComponentsInScreen){
		CGRect bounds = self.bounds;
		CGFloat componentWidth = bounds.size.width/maxComponentsInScreen;
		//使用component居中
		offset.x = (component-1)*componentWidth;
		offset.x = MAX(offset.x,self.containerView.mk_contentOffsetOfMinX);
		offset.x = MIN(offset.x,self.containerView.mk_contentOffsetOfMaxX);
	}
	[self.containerView setContentOffset:offset animated:YES];
}
@end

@implementation MKUIPickerViewModel(MKUITreePickerView)
- (void)setTreePickerViewDataSourceAndDelegate:(MKUITreePickerView *)treePickerView{
	treePickerView.dataSource = self;
	treePickerView.delegate = self;
	self.pickerView = treePickerView.pickerView;
}
@end
